package com.ailanyin.admin.modules.system.controller;

import com.ailanyin.admin.modules.system.model.SysJobLog;
import com.ailanyin.admin.modules.system.service.SysFileService;
import com.ailanyin.admin.modules.system.service.SysJobLogService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.ailanyin.common.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController
{
    @Autowired
    private SysJobLogService jobLogService;
    @Autowired
    private SysFileService sysFileService;

    /**
     * 查询定时任务调度日志列表
     */
    @PreAuthorize("hasAuthority('monitor:job:list')")
    @GetMapping("/list")
    public CommonPage list(SysJobLog sysJobLog,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
        return CommonPage.restPage(list);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @PreAuthorize("hasAuthority('monitor:job:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJobLog sysJobLog) throws Exception
    {
        List<SysJobLog> list = jobLogService.selectJobLogList(sysJobLog);
        String downloadFileName = ExcelUtil.exportExcel(list, "定时任务调度表", "定时任务调度", SysJobLog.class, "定时任务调度");
        sysFileService.fileDownload(downloadFileName, response);
    }

    /**
     * 根据调度编号获取详细信息
     */
    @PreAuthorize("hasAuthority('monitor:job:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long jobLogId)
    {
        return AjaxResult.success(jobLogService.selectJobLogById(jobLogId));
    }


    /**
     * 删除定时任务调度日志
     */
    @PreAuthorize("hasAuthority('monitor:job:remove')")
    @DeleteMapping("/{jobLogIds}")
    public AjaxResult remove(@PathVariable Long[] jobLogIds)
    {
        return AjaxResult.toResult(jobLogService.deleteJobLogByIds(jobLogIds));
    }

    /**
     * 清空定时任务调度日志
     */
    @PreAuthorize("hasAuthority('monitor:job:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        jobLogService.cleanJobLog();
        return AjaxResult.success();
    }
}
